package com.msconfig.mvp.view.adapter;

/*
 * author: caojunhao
 * date  : 2020/5/20
 * desc  :
 */

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.geo.msconfig.R;
import com.geo.parse.GnssSateInfo;
import com.geo.parse.GnssSateInfoList;

public class SatelliteFormAdapter extends BaseAdapter {
    private Context mContext;
    private GnssSateInfoList mSateInfoList;

    public SatelliteFormAdapter(Context context, GnssSateInfoList sateInfoList){
        this.mContext=context;
        this.mSateInfoList=sateInfoList;
    }

    @Override
    public int getCount() {
        return (mSateInfoList!=null && mSateInfoList.getSateInfoList()!=null) ? (int)mSateInfoList.getSateInfoList().size():0;
    }

    @Override
    public Object getItem(int position) {
        return mSateInfoList.getSateInfoList().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        String satelliteType="";
        String snr2,snr3;
        if(convertView==null){
            convertView= LayoutInflater.from(mContext).inflate(R.layout.layout_satellite_form_list_item,parent,false);
            viewHolder=new ViewHolder();
            viewHolder.relativeLayout = (RelativeLayout) convertView.findViewById( R.id.parent);
            viewHolder.tvSatelliteId = (TextView)convertView.findViewById( R.id.tv_satellite_id);
            viewHolder.tvElevation = (TextView)convertView.findViewById( R.id.tv_elevation );
            viewHolder.tvAzimuth = (TextView)convertView.findViewById( R.id.tv_azimuth );
            viewHolder.tvSnr1 = (TextView)convertView.findViewById( R.id.tv_snr1 );
            viewHolder.tvSnr2 = (TextView)convertView.findViewById( R.id.tv_snr2 );
            viewHolder.tvSnr3 = (TextView)convertView.findViewById( R.id.tv_snr3 );
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        GnssSateInfo gnssSateInfo=mSateInfoList.getSateInfoList().get(position);
        switch (gnssSateInfo.getType().swigValue()){
            case 1://SATSYS_GPS
                satelliteType="G";
                viewHolder.relativeLayout.setBackgroundColor(ContextCompat.getColor(mContext,R.color.gpscolor));
                break;
            case 2://SATSYS_GLONASS
                satelliteType="R";
                viewHolder.relativeLayout.setBackgroundColor(ContextCompat.getColor(mContext,R.color.glonasscolor));
                break;
            case 3://SATSYS_SBAS
                break;
            case 4://SATSYS_GALILEO
                satelliteType="E";
                viewHolder.relativeLayout.setBackgroundColor(ContextCompat.getColor(mContext,R.color.galileocolor));
                break;
            case 5://SATSYS_BD
                satelliteType="C";
                viewHolder.relativeLayout.setBackgroundColor(ContextCompat.getColor(mContext,R.color.beidoucolor));
                break;
            case 6://SATSYS_QZSS
                break;
        }

        //未解算--灰色
        if(gnssSateInfo.getStatus().swigValue()!=2){
            viewHolder.relativeLayout.setBackgroundColor(ContextCompat.getColor(mContext,R.color.nusedcolor));
        }
        viewHolder.tvSatelliteId.setText(mContext.getString(R.string.form_satellite_id,satelliteType, String.valueOf(gnssSateInfo.getPrn())));
        viewHolder.tvAzimuth.setText(String.valueOf(gnssSateInfo.getAzimuth()));
        viewHolder.tvElevation.setText(String.valueOf(gnssSateInfo.getElevation()));
        viewHolder.tvSnr1.setText(String.valueOf(gnssSateInfo.getSnr1()));
        if((int)gnssSateInfo.getSnr2()==0){
            snr2=mContext.getString(R.string.text_snr_none);
        }else{
            snr2= String.valueOf(gnssSateInfo.getSnr2());
        }

        if((int)gnssSateInfo.getSnr3()==0){
            snr3=mContext.getString(R.string.text_snr_none);
        }else{
            snr3= String.valueOf(gnssSateInfo.getSnr3());
        }
        viewHolder.tvSnr2.setText(snr2);
        viewHolder.tvSnr3.setText(snr3);

        return convertView;
    }

    public class ViewHolder{
        private RelativeLayout relativeLayout;
        private TextView tvSatelliteId;
        private TextView tvElevation;
        private TextView tvAzimuth;
        private TextView tvSnr1;
        private TextView tvSnr2;
        private TextView tvSnr3;
    }
}


